package customview.ike.com.customviewproject.widget.Refresh;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import customview.ike.com.customviewproject.R;

import static customview.ike.com.customviewproject.widget.Refresh.PullToRefreshLayout.HeaderStatus.PULL_TO_REFRESH;
import static customview.ike.com.customviewproject.widget.Refresh.PullToRefreshLayout.HeaderStatus.RELEASE_TO_REFRESH;


/**
 *@author:ike
 *@function: 加载更多尾布局
 *@creat time:16:52 2017/11/27
 **/

public class RotationArrowFooterLayout extends LinearLayout implements RefreshFooter {
    private String Tag="RotationArrowFooterLayout";
    private ImageView iv_progress;
    private ImageView iv_arrow;
    private TextView tv_status;
    private View rootView;
    private ProgressDrawable progressDrawable;
    private PullToRefreshLayout.HeaderStatus lastStatus;
    private RelativeLayout parent;
    public void setBg(int color){
        parent.setBackgroundColor(getContext().getResources().getColor(color));
    }
    public RotationArrowFooterLayout(Context context) {
        this(context,null);
    }

    public RotationArrowFooterLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public RotationArrowFooterLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        rootView = View.inflate(context, R.layout.layout_rotation_arrow_footer, this);
        initView();
    }

    private void initView() {
        parent= (RelativeLayout) findViewById(R.id.parent);
        progressDrawable = new ProgressDrawable();
        iv_progress = (ImageView) rootView.findViewById(R.id.iv_progress);
        iv_progress.setImageDrawable(progressDrawable);
        iv_arrow = (ImageView) rootView.findViewById(R.id.iv_arrow);
        tv_status = (TextView) rootView.findViewById(R.id.tv_status);
    }

    @Override
    public void PullToLoadMore(int scrollHeight, int headerHeight) {
        
    }

    @Override
    public void releaseToLoadMore(int offsetHeight, int headerHeight) {

    }

    @Override
    public void onStatusChanged(PullToRefreshLayout.HeaderStatus status) {
        switch (status) {
            case PULL_TO_REFRESH:
                if (lastStatus== RELEASE_TO_REFRESH){
                    rotationArrowAnimation(180,360);
                }

                tv_status.setText("上拉加载");
                iv_arrow.setVisibility(VISIBLE);
                iv_progress.setVisibility(GONE);
                progressDrawable.stop();
                break;
            case REFRESHING:
                tv_status.setText("刷新中...");
                iv_arrow.setVisibility(GONE);
                iv_progress.setVisibility(VISIBLE);
                progressDrawable.start();
                break;
            case RELEASE_TO_REFRESH:
                if (lastStatus==PULL_TO_REFRESH){
                    rotationArrowAnimation(0,180);
                }
                tv_status.setText("释放加载");
                iv_arrow.setVisibility(VISIBLE);
                iv_progress.setVisibility(GONE);
                progressDrawable.stop();
                break;
            default:
                break;

        }
        lastStatus=status;

    }

    @Override
    public int getContentViewHeight() {
        return DensityUtil.dp2px(100f);
    }

    @Override
    public View getContentView() {
        return this;
    }
    public void rotationArrowAnimation(int start, int end){

        ObjectAnimator.ofFloat(iv_arrow,"rotation",start,end).start();
    }
}
